; (function (window, $) {
  var dict = {
    birthday: function (timestamp) {
      var date = new Date(timestamp)
      return date.getFullYear() + '-' + parseInt(date.getMonth() + 1)
    },
    createTime: function (str) {
      return str.split(' ')[0]
    },
    sex: function (index) {
      return ['男', '女'][index]
    },
    carType: function (index) {
      return ['有车', '无车'][index]
    },
    houseType: function (index) {
      return ['有房', '无房'][index]
    },
    marryType: function (index) {
      return ['未婚', '已婚'][index]
    },
    credit: function (index) {
      return ['优秀', '良好', '一般'][index]
    },
    education: function (index) {
      return ['小学', '初中', '高中', '大学', '本科', '硕士', '博士'][index]
    },
    professional: function (index) {
      return ['自由职业者', '个体户', '企业主', '上班族'][index]
    },
    headship: function (index) {
      return ['渠道专员', '渠道主管', '渠道经理'][index]
    },
    salary: function (index) {
      return ['2000以下', '2000-5000', '5000-10000', '10000以上'][index]
    },
    headship: function (index) {
      return ['员工', '主管', '中层领导', '高层领导', 'CEO'][index]
    }
  }
  var formobj = {}
  var oldimg = ''

  $(function () {
    //uploadbtn
    $('input[type="file"]').on('change', function () {
      var fileInput = $(this)

      util.fileUpload(fileInput[0].files[0], function (res) {
        fileInput.prev('div').text('重新上传')
        fileInput.next('input[type="hidden"]').val(res.data)
        oldimg = $('#profile-headshow').attr('src')
        $('#profile-headshow').attr('src', res.data)
      })
    })

    getInfo()

    $('a.edit-btn').on('click', edit)
    $('a#save-btn').on('click', save)
    $('a#cancel-btn').on('click', cancel)
  })

  function getInfo() {
    util.ajax({
      url: '/user/member',
      type: 'GET',
      success: function (res) {
        renderInfo(res.data)
      }
    })
  }

  function renderInfo(obj) {
    formobj = obj
    $('span.field').each(function () {
      var key = $(this).data('field')
      if ($(this).data('dict')) {
        if (dict[key] && typeof formobj[key] !== 'undefined') {
          $(this).text(dict[key](formobj[key]))
        }
      } else {
        if (formobj[key]) {
          $(this).text(formobj[key])
        }
      }
    })

    $('#profile-headshow').attr(
      'src',
      formobj.avatar || '/assets/images/default-avatar.png'
    )
  }
  //点击修改
  function edit() {
    $('#edit-panel').show()
    $('#headshow-upload').show()
    $('#profile-panel').hide()

    $('select.formobj').each(function () {
      var key = $(this).attr('name')
      $(this).val(formobj[key])
    })

    $("input[type='text'].formobj").each(function () {
      var key = $(this).attr('name')
      $(this).val(formobj[key])
    })

    $("select[name='year']").val(new Date(formobj.birthday).getFullYear())
    var month = new Date(formobj.birthday).getMonth() + 1
    monthStr = parseInt(month) < 10 ? '0' + month : String(month)
    console.log(monthStr)

    $("select[name='month']").val(monthStr)

    $("input[name='sex'][value='" + formobj['sex'] + "']").prop('checked', true)
    $("input[name='marryType'][value='" + formobj['marryType'] + "']").prop(
      'checked',
      true
    )

    $("input[name='avatar']").val(formobj['avatar'])
  }

  //点击保存
  function save() {
    var formobj = {}
    var vali = true

    // input type=text: check and add
    $("input[type='text'].formobj").each(function () {
      if (!vali) {
        return
      }
      if ($(this).val() === '') {
        // alert($(this).data('label') + '未填写')
        layer.alert($(this).data('label') + '未填写', {
          skin: 'demo-class',
          closeBtn: 0
        })
        vali = false
      }
      var key = $(this).attr('name')
      formobj[key] = $(this).val()
    })

    // select: check and add
    $('select.formobj').each(function () {
      if (!vali) {
        return
      }
      if (parseInt($(this).val()) === -1 || $(this).val() === null) {
        // alert($(this).data('label') + '未填写')
        layer.alert($(this).data('label') + '未填写', {
          skin: 'demo-class',
          closeBtn: 0
        })
        vali = false
      }
      var key = $(this).attr('name')
      formobj[key] = $(this).val()
    })

    if (!vali) {
      return
    }

    formobj.sex = $('input[name="sex"]:checked').val()
    formobj.marryType = $('input[name="marryType"]:checked').val()

    formobj.birthday =
      $("select[name='year']").val() +
      '-' +
      $("select[name='month']").val() +
      '-01'

    formobj.avatar = $("input[name='avatar']").val()

    console.log(formobj)

    // del field
    delete formobj.id
    delete formobj.status
    delete formobj.userType
    delete formobj.phone
    delete formobj.isProfile
    delete formobj.createTime

    // some faker data

    // formobj.cityName = '南京'
    // formobj.provinceName = '江苏'

    util.ajax({
      url: '/member/profile/save',
      type: 'POST',
      data: formobj,
      success: function (res) {
        console.log(res)
        $('#edit-panel').hide()
        getInfo()
        $('#headshow-upload').hide()
        $('#profile-panel').show()
        window.flushInfo()
      }
    })
  }

  function cancel() {
    $('#edit-panel').hide()
    $('#headshow-upload').hide()
    $('#profile-panel').show()
    if (oldimg) {
      $('#profile-headshow').attr('src', oldimg)
    }
  }
})(window, jQuery)
